package com.beijiteshop.shop.ui.coupon.fragment

import android.content.Context
import android.os.Bundle
import android.util.TypedValue
import androidx.fragment.app.Fragment
import android.view.View
import androidx.core.content.ContextCompat
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.shop.R
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentCouponBinding
import com.beijiteshop.shop.ui.main.FragmentPagerAdapter
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 * 优惠券页面
 */
class CouponFragment : BaseContainerFragment(R.layout.fragment_coupon) {

   companion object{
       const val BUNDLE_TYPE = "bundle_type"

       fun newInstance(index: Int): CouponFragment{
           return CouponFragment().apply {
               arguments = Bundle().apply {
                   putInt(BUNDLE_TYPE,index)
               }
           }
       }
   }

    private val mBinding by bindView<FragmentCouponBinding>()
    private var mTitles = listOf<String>()
    private var mFragmentList = mutableListOf<Fragment>()
    private var mType = 0

    override fun initView(view: View) {
        mTitles = resources.getStringArray(R.array.coupon_tabs).toList()
        mType = arguments?.getInt(BUNDLE_TYPE)?: 0

        mBinding.couponTitleBar.titleBarTitleTv.text = resources.getString(R.string.coupon)

        initFragments()
        initIndicator()
        initViewPager()
    }

    override fun initData() {

    }

    override fun initEvent() {
        mBinding.couponTitleBar.apply {
            titleBarBackIv.setOnClickListener {
                close()
            }
        }
    }

    override fun onLazyLoad() {

    }

    private fun initFragments(){
        mFragmentList.add(CouponListFragment.newInstance(0))
        mFragmentList.add(CouponListFragment.newInstance(1))
        mFragmentList.add(CouponListFragment.newInstance(2))
    }

    private fun initIndicator(){
        mBinding.couponIndicator.apply {
            val commonNavigator = CommonNavigator(requireContext())
                .apply {
                    adapter = object : CommonNavigatorAdapter(){
                        override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                            val simplePagerTitleView: SimplePagerTitleView =
                                ColorTransitionPagerTitleView(context)
                            simplePagerTitleView.text = mTitles[index]
                            simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14f)
                            simplePagerTitleView.normalColor = ContextCompat.getColor(requireContext(),R.color.text_light_dark)
                            simplePagerTitleView.selectedColor = ContextCompat.getColor(requireContext(),R.color.color_primary)
                            simplePagerTitleView.setOnClickListener {
                                mBinding.couponVp.currentItem = index
                            }
                            return simplePagerTitleView
                        }

                        override fun getCount(): Int {
                            return mTitles.size
                        }

                        override fun getIndicator(context: Context?): IPagerIndicator {
                            val indicator = LinePagerIndicator(context)
                            indicator.setColors(ContextCompat.getColor(requireContext(),R.color.color_primary))
                            return indicator
                        }
                    }
                }
            this.navigator = commonNavigator
            ViewPagerHelper.bind(this,mBinding.couponVp)
        }
    }

    private fun initViewPager(){
        mBinding.couponVp.apply {
            adapter = FragmentPagerAdapter(mFragmentList,childFragmentManager)
            currentItem = mType
        }
    }

}